package frontend.pages.reports.base;

import backend.model.worklog.WorkLog;
import frontend.pages.worklog.daily.DefaultWorkLogTableTemplate;
import frontend.pages.worklog.daily.WorkLogTable;
import org.apache.wicket.Component;
import org.apache.wicket.extensions.markup.html.repeater.data.table.DefaultDataTable;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;

public abstract class AbstractWorklogReportPanel extends AbstractReportPanel<WorkLog> {
    private static final String RESOURCE_KEY_FIELD_LABEL_TOTAL_HOURS = "report.totalHours";
    private Model totalHoursModel = new Model(0);
    protected Label totalHoursLabel;

    public AbstractWorklogReportPanel(String id) {
        super(id);
    }

    @Override
    protected DefaultDataTable<WorkLog> getReportResultsTable() {
        return new WorkLogTable("reportResultsTable", reportResults, getTableTemplate());
    }

    protected boolean shouldAddTotalHours() {
        return true;
    }

    protected Component createTotalHoursPanel() {
        totalHoursLabel = new Label("totalHours", new StringResourceModel(RESOURCE_KEY_FIELD_LABEL_TOTAL_HOURS, totalHoursModel));
        totalHoursLabel.setOutputMarkupId(true);
        return totalHoursLabel;
    }

    private int getTotalHours() {
        int totalHours = 0;
        if (reportResults != null) {
            for (WorkLog workLog : reportResults) {
                totalHours += workLog.getHours();
            }
        }
        return totalHours;
    }

    public void updateTotalHoursModel() {
        totalHoursLabel.setDefaultModel(new StringResourceModel(RESOURCE_KEY_FIELD_LABEL_TOTAL_HOURS, new Model(getTotalHours())));
    }

    protected abstract DefaultWorkLogTableTemplate getTableTemplate();
}
